package com.wl.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;

public class AServlet extends HttpServlet {

    // 处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("AServlet get...");
        // 1.设置请求的字符编码
        req.setCharacterEncoding("utf-8");
        // 2.客户端信息
        System.out.println("--------------客户来了--------------");
        System.out.println("客户IP:" + req.getRemoteHost());
        System.out.println("客户PORT:" + req.getRemotePort());
        System.out.println("请求方式:" + req.getMethod());
        System.out.println("请求路径:" + req.getRequestURL());
        // 3.获取参数   http://localhost:8080/a?name=zs&hb=1&hb=2
        String name = req.getParameter("name");
        System.out.println("参数name:" + name);
        String[] hbs = req.getParameterValues("hb");
        System.out.println("参数hbs:" + Arrays.toString(hbs));

        // 4.设置属性
        req.setAttribute("version","v1.0");
        req.setAttribute("author","wl");

        System.out.println(req.getAttribute("version"));
        System.out.println(req.getAttribute("author"));

//        req.removeAttribute("version");
//        req.removeAttribute("author");

        // 5.转发给C
        req.getRequestDispatcher("/c").forward(req,resp);

    }

    // 处理post请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 调用doGet方法
        doGet(req,resp);
    }
}
